package org.tp8.sampleapp.jaxb.paging.dto;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.tp8.sampleapp.jaxb.paging.adapter.SortAdapter;

/**
 * The Class PageResponse.
 */
@SuppressWarnings("rawtypes")
public class PageResponse implements Page {

	/**
	 * The pojo.
	 */
	private final JaxbPage pojo;

	/**
	 * Instantiates a new page response.
	 * 
	 * @param pojo
	 *            the pojo
	 */
	public PageResponse(JaxbPage pojo) {
		this.pojo = pojo;
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#getContent()
	 */
	public List getContent() {
		List<?> result;
		if (pojo.getContent() != null) {
			result = pojo.getContent();
		} else {
			result = Collections.EMPTY_LIST;
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#getNumber()
	 */
	public int getNumber() {
		return pojo.getNumber();
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#getNumberOfElements()
	 */
	public int getNumberOfElements() {
		return pojo.getNumberOfElements();
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#getSize()
	 */
	public int getSize() {
		return pojo.getSize();
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#getSort()
	 */
	public Sort getSort() {
		try {
			return SortAdapter.getInstance().unmarshal(pojo.getSort());
		} catch (Exception e) {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#getTotalElements()
	 */
	public long getTotalElements() {
		return pojo.getTotalElements();
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#getTotalPages()
	 */
	public int getTotalPages() {
		return pojo.getTotalPages();
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#hasContent()
	 */
	public boolean hasContent() {
		return getContent().size() > 0;
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#hasNextPage()
	 */
	public boolean hasNextPage() {
		return false;
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#hasPreviousPage()
	 */
	public boolean hasPreviousPage() {
		return false;
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#isFirstPage()
	 */
	public boolean isFirstPage() {
		return false;
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#isLastPage()
	 */
	public boolean isLastPage() {
		return false;
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#iterator()
	 */
	public Iterator iterator() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#nextPageable()
	 */
	@Override
	public Pageable nextPageable() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.domain.Page#previousPageable()
	 */
	@Override
	public Pageable previousPageable() {
		return null;
	}

}